package com.hbm.usercenter.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hbm.usercenter.MainActivity;
import com.hbm.usercenter.R;
import com.hbm.usercenter.base.BaseFragmentCommon;
import com.hbm.usercenter.conf.Constants;
import com.hbm.usercenter.http.MyCallBack;
import com.hbm.usercenter.http.NetClient;
import com.hbm.usercenter.utils.CommonUtils;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;
import com.hbm.usercenter.widget.GroupInput;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragmentCommon {


    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.et_username)
    GroupInput mEtUsername;
    @Bind(R.id.et_pwd)
    GroupInput mEtPwd;
    @Bind(R.id.btn_login)
    Button mBtnLogin;
    private static String mUsername;
    private String mPwd;

    public static String getName() {
        return  mUsername;
    }

    public static void setNull() {
        mUsername = null;
    }

    public LoginFragment() {
        // Required empty public constructor
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        mToolbarTitle.setText("惠学科技会员登录");

        long leastMillis = 1495163413;
        Date date = new Date(leastMillis);
        // 获取日期
        System.out.println(date.toLocaleString()); // 2014-3-5 9:42:13
        System.out.println(date.toString()); // 2014-03-05
    }



    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        mUsername = mEtUsername.getText();
        mPwd = mEtPwd.getText();
        sendRequest();
    }

    private void sendRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("username",mUsername);
        params.put("pwd",mPwd);
        NetClient.getNetClient().callNet(Constants.URLS.LOGINURL, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json);
                LogUtils.d(json);

            }
        },params);

    }

    private void processResult(String json) {
        JSONObject rootObject = null;
        try {
            rootObject = new JSONObject(json);
            final String err_msg = rootObject.getString("err_msg");
            String err_code = rootObject.getString("err_code");
            if (err_code.equals("-1")) {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(),err_msg);
                    }
                });
//                Intent intent = new Intent(getActivity(), QRCodeActivity.class);
//                intent.putExtra("money", number);
//                //                intent.putExtra("money", money);
//                //                intent.putExtra("picUrl", picUrl);
//                //
//                startActivity(intent);
                startActivity(new Intent(getActivity(), MainActivity.class));
                getActivity().finish();
            } else {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(),err_msg);
                    }
                });
//                Toast.makeText(UIUtils.getContext(), err_msg, Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


}
